package com.nf.dao;

import com.nf.entity.Students;
import com.nf.util.CloseHelp;
import com.nf.util.Utils;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class StudentsDaoImpl implements StudentDao{
    //获取数据库连接对象
    private Connection conn = Utils.getConnection();
    //定义ResultSet结果集对象
    private ResultSet rs = null;
    //定义一个PreparedStatement对象
    private PreparedStatement pds = null;

    //查询所有学生
    @Override
    public List<Students> queryAll() {
        List<Students> list = new ArrayList<Students>();
        try {
            pds = conn.prepareStatement("select * from students");
            rs = pds.executeQuery();
            while(rs.next()){
                Students stu = new Students();
                stu.setId(rs.getInt("id"));
                stu.setName(rs.getString("name"));
                stu.setSex(rs.getString("sex"));
                stu.setAge(rs.getInt("age"));
                stu.setBirthday(rs.getDate("birthday"));
                list.add(stu);
            }
            return list;
        } catch (SQLException e) {
            e.printStackTrace();
        }finally{
            CloseHelp.CloseRsPdsConn(rs,pds,conn);
        }
        return null;
    }

    //添加学生
    @Override
    public int addStudnt(Students students) {
        try {
            pds = conn.prepareStatement("insert into students(name,sex,age,birthday) values(?,?,?,?)");
            pds.setString(1,students.getName());
            pds.setString(2,students.getSex());
            pds.setInt(3,students.getAge());
            pds.setDate(4,new Date(students.getBirthday().getTime()));
            pds.executeUpdate();
            return 1;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally{
            CloseHelp.ClosePdsConn(pds,conn);
        }
        return 0;
    }

    //删除学生
    @Override
    public int delete(int id) {
        try {
            pds = conn.prepareStatement("delete from students where id=?");
            pds.setInt(1,id);
            pds.executeUpdate();
            return 1;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally{
            CloseHelp.ClosePdsConn(pds,conn);
        }
        return 0;
    }

    //修改学生信息
    @Override
    public int update(Students students) {
        try {
            pds = conn.prepareStatement("update students set name=?,sex=?,age=?,birthday=? where id=?");
            pds.setString(1,students.getName());
            pds.setString(2,students.getSex());
            pds.setInt(3,students.getAge());
            pds.setDate(4,new Date(students.getBirthday().getTime()));
            pds.setInt(5,students.getId());
            pds.executeUpdate();
            return 1;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally{
            CloseHelp.ClosePdsConn(pds,conn);
        }
        return 0;
    }

    //根据ID查询学生
    @Override
    public Students query(int id) {
        try {
            pds = conn.prepareStatement("select * from students where id=?");
            pds.setInt(1,id);
            rs = pds.executeQuery();
            if(rs.next()){
                Students stu = new Students();
                stu.setId(rs.getInt("id"));
                stu.setName(rs.getString("name"));
                stu.setSex(rs.getString("sex"));
                stu.setAge(rs.getInt("age"));
                stu.setBirthday(rs.getDate("birthday"));
                return stu;
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally{
            CloseHelp.CloseRsPdsConn(rs,pds,conn);
        }
        return null;
    }
}
